const formatTime = (date, index) => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  if (index == 0) {
    return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute].map(formatNumber).join(':')
  } else if (index == 1) {
    return [year, month, day].map(formatNumber).join('-')
  } else if (index == 2) {
    return [hour, minute].map(formatNumber).join(':')
  }
}

const judgeTime = (startDate, startTime, endDate, endTime) => {
  let t1 = startDate.replace(/-/g, "/") + " " + startTime
  let t2 = endDate.replace(/-/g, "/") + " " + endTime
  let start = new Date(t1)
  let end = new Date(t2)
  return start.getTime() <= end.getTime()
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

module.exports = {
  formatTime: formatTime,
  judgeTime: judgeTime
}